package com.xpgk.mvc.statistic.productStock;


import java.util.List;

import com.jfinal.kit.JsonKit;
import com.jfinal.plugin.activerecord.Record;
import com.xpgk.constant.ConstantInit;
import com.xpgk.mvc.base.BaseController;
import com.xpgk.mvc.organization.Organization;

public class ProductStockStatisticController extends BaseController {
	
	/**
	 * 当前库存select
	 */
	public static final String sqlId_splitPageSelect = "sys.productStockStatistic.splitPageSelect";
	
	/**
	 * 当前库存from
	 */
	public static final String sqlId_splitPageFrom = "sys.productStockStatistic.splitPageFrom";
	
	/**
	 * 历史库存select
	 */
	public static final String sqlId_historySplitPageSelect = "sys.productStockStatistic.historySplitPageSelect";
	
	/**
	 * 历史库存from
	 */
	public static final String sqlId_historySplitPageFrom = "sys.productStockStatistic.historySplitPageFrom";
	
	/**
	 * 出入库明细select
	 */
	public static final String sqlId_outinSplitPageSelect = "sys.productStockStatistic.outinSplitPageSelect";
	
	/**
	 * 出入库明细from
	 */
	public static final String sqlId_outinSplitPageFrom = "sys.productStockStatistic.outinSplitPageFrom";
	
	
	public void index() {
		setAttr("curOrgan", Organization.dao.findById(getCUserOrganIds()));
		render("productStockStatistic.html");
	}
	
	/**
	 * 当前库存
	 */
	public void view(){
		String json = paging(splitPage, sqlId_splitPageSelect, sqlId_splitPageFrom);
		renderJson(json);
		
	}
	
	/**
	 * 历史库存
	 */
	public void historyView(){
		String json = paging(splitPage, sqlId_historySplitPageSelect, sqlId_historySplitPageFrom);
		renderJson(json);
	}
	
	/**
	 * 跳转至出入库明细
	 */
	public void toOutinView(){
		keepPara();
		render("productStockOutin.html");
	}
	
	/**
	 * 出入库明细
	 */
	public void outinView(){
		paging(ConstantInit.db_dataSource_main, splitPage, sqlId_outinSplitPageSelect, sqlId_outinSplitPageFrom);
		List<Record> datas = (List<Record>) splitPage.getList();
		String json = JsonKit.toJson(datas);
		Long sum = 0l;
		for(Record r : datas){
			sum += r.getLong("number");
		}
		json = "{\"total\":" + splitPage.getTotalRow() + ",\"rows\":" + json + ",\"footer\":[{\"number\": "+ sum +",\"type\": \"库存变动(个)\"}]}";
		renderJson(json);
	}
	

}
